package com.sgb.kjwl.view.ui.activity.transport;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.mob.MobSDK;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityShareLogisticsBinding;
import com.sgb.kjwl.model.entity.ShareSDKUtilsEntity;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.MToast;

import java.util.HashMap;

import cn.sharesdk.dingding.friends.Dingding;
import cn.sharesdk.dingding.utils.DingdingClientNotExistException;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qq.QQClientNotExistException;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import cn.sharesdk.wechat.utils.WechatClientNotExistException;

/**
 * @author 黄淋
 */
public class ShareLogisticsActivity extends Activity {

    ActivityShareLogisticsBinding binding;
    String title;
    String content;
    String imageUrl;
    String url;
    boolean isCar;
    Bitmap imageBitmap;

    public static void start(Context context, String title, String content, String imageUrl, String url, boolean isCar) {
        Intent intent = new Intent();
        intent.putExtra("title", title);
        intent.putExtra("content", content);
        intent.putExtra("imageUrl", imageUrl);
        intent.putExtra("url", url);
        intent.putExtra("isCar", isCar);
        intent.setClass(context, ShareLogisticsActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_share_logistics);
        title = getIntent().getStringExtra("title");
        content = getIntent().getStringExtra("content");
        imageUrl = getIntent().getStringExtra("imageUrl");
        url = getIntent().getStringExtra("url");
        isCar = getIntent().getBooleanExtra("isCar", false);
        if (isCar) {
            imageBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_car_manager_default);
        }
        initView();

    }

    private void initView() {
        binding.tvDingding.setOnClickListener(v -> {
            ShareSDKUtilsEntity bean = new ShareSDKUtilsEntity();
            bean.setTitle(title);
            bean.setText(content);
            if (TextUtils.isEmpty(imageUrl) && isCar) {
                bean.setImageBitmap(imageBitmap);
            } else {
                bean.setImageUrl(imageUrl);
            }
            bean.setUrl(url);
            bean.setShareType(Dingding.NAME);
            bean.setToastText("钉钉");
            bean.setToastShow(true);
            shareShow(ShareLogisticsActivity.this, bean);
        });
        binding.tvWechat.setOnClickListener(v -> {
            ShareSDKUtilsEntity bean = new ShareSDKUtilsEntity();
            bean.setTitle(title);
            bean.setText(content);
            if (TextUtils.isEmpty(imageUrl) && isCar) {
                bean.setImageBitmap(imageBitmap);
            } else {
                bean.setImageUrl(imageUrl);
            }
            bean.setUrl(url);
            bean.setShareType(Wechat.NAME);
            bean.setToastText("微信");
            bean.setToastShow(true);
            shareShow(ShareLogisticsActivity.this, bean);
        });
        binding.tvWechatMoments.setOnClickListener(v -> {
            ShareSDKUtilsEntity bean = new ShareSDKUtilsEntity();
            bean.setTitle(title);
            bean.setText(content);
            if (TextUtils.isEmpty(imageUrl) && isCar) {
                bean.setImageBitmap(imageBitmap);
            } else {
                bean.setImageUrl(imageUrl);
            }
            bean.setUrl(url);
            bean.setShareType(WechatMoments.NAME);
            bean.setToastText("朋友圈");
            bean.setToastShow(true);
            shareShow(ShareLogisticsActivity.this, bean);
        });
        binding.tvQq.setOnClickListener(v -> {
            ShareSDKUtilsEntity bean = new ShareSDKUtilsEntity();
            bean.setTitle(title);
            bean.setText(content);
            if (TextUtils.isEmpty(imageUrl) && isCar) {
                bean.setImageBitmap(imageBitmap);
            } else {
                bean.setImageUrl(imageUrl);
            }
            bean.setUrl(url);
            bean.setShareType(QQ.NAME);
            bean.setToastText("QQ");
            bean.setToastShow(true);
            shareShow(ShareLogisticsActivity.this, bean);
        });
        binding.tvCopy.setOnClickListener(v -> {
            //获取剪贴板管理器：
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", url);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            MToast.showToast(ShareLogisticsActivity.this, "复制成功");
            finish();
        });
        binding.tvCancel.setOnClickListener(v -> {
            finish();
        });
        binding.llGrayLayout.setOnClickListener(v -> finish());
    }


    public void shareShow(Context context, ShareSDKUtilsEntity shareBean) {
        MobSDK.submitPolicyGrantResult(true, null);
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setTitle(shareBean.getTitle());
        sp.setText(shareBean.getText());
        sp.setImageUrl(shareBean.getImageUrl());
        sp.setImageData(shareBean.getImageBitmap());
        //qq平台需要
        sp.setTitleUrl(shareBean.getUrl());
        sp.setUrl(shareBean.getUrl());
        sp.setShareType(Platform.SHARE_WEBPAGE);
        Platform qzone = ShareSDK.getPlatform(shareBean.getShareType());
        if (qzone == null) {
            MToast.showToast(ShareLogisticsActivity.this, "分享服务升级中，暂不可用~");
            return;
        }
        if (!qzone.isClientValid()) {
            if (Dingding.NAME.equals(shareBean.getShareType())) {
                MToast.showToast(context, "您未安装钉钉客户端，分享失败！");
            } else if (Wechat.NAME.equals(shareBean.getShareType())) {
                MToast.showToast(context, "您未安装微信客户端，分享失败！");
            } else if (WechatMoments.NAME.equals(shareBean.getShareType())) {
                MToast.showToast(context, "您未安装微信客户端，分享失败！");
            } else if (QQ.NAME.equals(shareBean.getShareType())) {
                MToast.showToast(context, "您未安装QQ客户端，分享失败！");
            }
            return;
        }
        // 设置分享事件回调（注：回调放在不能保证在主线程调用，不可以在里面直接处理UI操作）
        qzone.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                //失败的回调，arg:平台对象，arg1:表示当前的动作，arg2:异常信息
            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap arg2) {
                Log.e("tag", "分享成功................");
                //分享成功的回调
                MToast.showToast(context, "分享成功！");
                ShareLogisticsActivity.this.finish();
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
                MToast.showToast(context, "分享取消！");
                ShareLogisticsActivity.this.finish();
            }
        });
        // 执行图文分享
        qzone.share(sp);
    }
}
